<template>
  <div class="flex h-full">
    <div class="flex flex-1 overflow-hidden">
      <a-card class="flex-1 overflow-hidden" :body-style="{ padding: '0 8px', height: '100%', overflow: 'hidden' }">
        <div class="flex justify-between mt-2">
          <div class="flex">
            <a-radio-group v-model:value="tabsKey" button-style="solid" @change="onChangeTab">
              <a-radio-button :value="1">类别费用</a-radio-button>
              <a-radio-button :value="2">明细</a-radio-button>
            </a-radio-group>
            <!-- 明细 -->
            <a-input-search v-if="tabsKey === 2" v-model:value="pyjm" allow-clear class="ml-2 w-56 mr-4" placeholder="项目名称/助记码" @search="getData" />
            <div v-if="tabsKey === 2" class="flex items-center ml-4">
              <a-radio-group v-model:value="category" @change="getData()">
                <a-radio value="">全部</a-radio>
                <a-radio :value="0">临时</a-radio>
                <a-radio :value="1">长期</a-radio>
                <a-radio :value="2">出院带药</a-radio>
              </a-radio-group>
            </div>
            <div v-if="tabsKey === 2" class="flex items-center ml-4">
              <span class="mr-2">合并显示</span>
              <a-switch v-model:checked="merge" @change="getData" />
            </div>
          </div>
          <a-space v-if="tabsKey === 2">
            <!-- <BaseExport
              type="住院"
              name="费用核对"
              :code="merge ? '000676' : '000110'"
              :search-params="{ inplsh: store.currentPatient.inplsh, pyjm: pyjm, advExecType: category }"
              >导出</BaseExport
            > -->
          </a-space>
        </div>
        <!-- 类别费用 -->
        <div v-if="tabsKey === 1" class="w-500">
          <BaseTable class="mt-2" :loading="loading" column-code="000109" :data-source="dataCategory" bordered>
            <template #bodyCell="{ column, index }">
              <template v-if="column.dataIndex === 'xh'">
                {{ index + 1 }}
              </template>
            </template>
            <template v-if="tabsKey === 1" #summary>
              <a-table-summary fixed>
                <a-table-summary-row>
                  <a-table-summary-cell>-</a-table-summary-cell>
                  <a-table-summary-cell class="text-right">
                    <a-typography-text>合计</a-typography-text>
                  </a-table-summary-cell>
                  <a-table-summary-cell class="text-right">
                    <a-typography-text>{{ dataTotal ? dataTotal.toFixed(2) : '' }}</a-typography-text>
                  </a-table-summary-cell>
                </a-table-summary-row>
              </a-table-summary>
            </template>
          </BaseTable>
        </div>
        <!-- 明细 -->
        <div v-else>
          <div v-if="!merge">
            <BaseTable class="mt-2" :loading="loading" column-code="000110" :scroll="{ x: 1900 }" :data-source="dataSource" fill>
              <template #bodyCell="{ column, record, index }">
                <template v-if="column.dataIndex === 'xh'">
                  {{ index + 1 }}
                </template>
                <template v-if="column.dataIndex === 'advExecTypeStr'">
                  {{ record.advExecTypeStr || '-' }}
                </template>
                <template v-if="column.dataIndex === 'adv'">
                  <DoctorAdviceOrder
                    :row-data="record"
                    :params-key="{ mc: 'advName', sl: 'clinicalDose', dw: 'clinicalDoseUnit', gg: 'spc', yf: 'fyff', pl: 'zjm' }"
                  />
                </template>
                <template v-if="column.dataIndex === 'status'">
                  <a-tag v-if="record.status === 2" color="blue">未审核</a-tag>
                  <a-tag v-else-if="record.status === 3" color="green">已审核</a-tag>
                  <span v-else>-</span>
                </template>
                <template v-if="column.dataIndex === 'action'">
                  <!-- chargeStatus 0:未结账|1：已结账 |2：退费中 -->
                  -
                </template>
              </template>
            </BaseTable>
          </div>
          <div v-else>
            <BaseTable class="mt-2" :loading="loading" column-code="000676" :data-source="mergeTable" fill>
              <template #bodyCell="{ column, record, index }">
                <template v-if="column.dataIndex === 'xh'">
                  {{ index + 1 }}
                </template>
                <template v-if="column.dataIndex === 'advTypeName'">
                  <span class="pr-2">{{ record.advTypeName }}</span>
                  <span v-if="record.gg" class="pr-2 text-gray-400 text-xs">{{ record.gg }}</span>
                  <span v-if="record.cj" class="pr-2 text-gray-400 text-xs">{{ record.cj }}</span>
                </template>
              </template>
            </BaseTable>
          </div>
        </div>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../../store'

const store = useStore()
const columnCodeR = ref('000342')
const loading = ref(false)
const dataSource = ref([])
const dataCategory = ref([])
const tabsKey = ref(1)
const pyjm = ref(undefined)
const merge = ref(true)
const category = ref('')
const dataTotal = ref(0)
const allAuditFlag = ref(true)
const mergeTable = ref([])
const allAuditDisabled = ref(0)

//获取数据
const getData = async () => {
  loading.value = true
  if (tabsKey.value === 2) {
    const res = await post('/inp/inpComprehensive/getMergePatientBillList', {
      inplsh: store.currentPatient.inplsh,
      pyjm: pyjm.value,
      advExecType: category.value
    })
    if (res.code !== 0) return message.error(res.msg || '获取数据失败')
    mergeTable.value = res.data
  }
  //类别费用
  const res = await post('/inp/inpComprehensive/getPatientBillList', {
    inplsh: store.currentPatient.inplsh,
    pyjm: pyjm.value,
    advExecType: category.value,
    statusList: [2, 3]
  })
  loading.value = false
  if (res.code !== 0) return message.error(res.msg || '获取数据失败')
  dataSource.value = res.data.patientBillList || []
  allAuditDisabled.value = res.data.chargeStatus
  dataSource.value = dataSource.value.map((it) => {
    return { ...it, advOrderType: 0 }
  })
  dataCategory.value = res.data.patientBillTypeList
  dataTotal.value = res.data.amount
  const flag = dataSource.value.filter((it) => {
    return it.status === 2
  })
  if (flag.length > 0 || dataSource.value.length === 0) {
    allAuditFlag.value = true
  } else {
    allAuditFlag.value = false
  }
}
const onChangeTab = () => {
  getData()
}
watch(
  () => store.currentPatient.inplsh,
  (val) => {
    if (val) getData()
  },
  { immediate: true, deep: true }
)
onMounted(() => {
  if (getSystemConfig('INP_CHARGE_ENTER_ACCOUNT_MODE') === '0') {
    //住院费用依据
    columnCodeR.value = '000342'
  } else {
    columnCodeR.value = '000344'
  }
})
</script>
<style lang="less" scoped>
.w-500 {
  width: 500px;
}
.bg-color {
  background: var(--thin-color);
}
:deep(.ant-table-row-expand-icon) {
  display: none;
}
.classify-checkall {
  .ant-tag {
    font-size: 14px;
    line-height: 30px;
    padding-inline: 14px;
  }
}
</style>
